import CURDHelper from '@/utils/CURDHelper'
import PageConfig from '@/utils/pageConfig'
import { getBtnCtr } from '@/utils/BtnControl'
import Query from '@/components/Query';
import { isNumber } from '@/utils/validate';
import { VxeUI } from 'vxe-pc-ui';
import XEUtils from 'xe-utils';
export default {
  name: 'ProjectReceive',
  components: {
    Query
  },
  data() {
    const skztItemRender = {
      name: 'VxeSelect',
      options: [
        { label: '未收款', value: 'N' },
        { label: '已收款', value: 'Y' }
      ],
    }
    return {
      skztItemRender,
      componentSize: PageConfig.ComponentSize,
      duration: PageConfig.Duration,
      btnPromission: '',
      editModal: false,
      QueryConditions: [],
      BaseApiUrl: '/api/Xmglxmxx',
      BaseChildApiUrl: '/api/Xmglxmxxsk',
      editRow: undefined,
      CalendarView: false,
      QueryConfig: [{
        title: '客户名称',
        field: 'khmc',
        type: 'input',
        value: '',
        placeholder: '请输入客户名称以查询',
      }, {
        title: '体系名称',
        field: 'khmc',
        type: 'input',
        value: '',
        placeholder: '请输入体系名称以查询',
      }, {
        title: '签单日期',
        field: 'htqdrq',
        type: 'daterange',
        date1: undefined,
        date2: undefined,
        placeholder: ''
      }, {
        title: '收款状态',
        field: 'skzt',
        type: 'select',
        value: 'N',
        placeholder: '',
        options: [
          { label: '未收款', value: 'N' },
          { label: '已收款', value: 'Y' }
        ]
      },],
      GridOptions: {
        id:'ProjectReceiveMainGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 400,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true,
          isCurrent:true
        },
        checkboxConfig: {
          highlight: true,
          range: true
        },
        exportConfig: {
        },
        toolbarConfig: {
          custom: true,
        },
        customConfig:{
          mode:'drawer',
          storage:true
        },
        columns: [
          { type: 'seq', width: 50 },
          { type: 'checkbox', width: 35 },
          { field: 'txid', title: '体系ID', visible: false },
          { field: 'khmc', title: '客户名称', sortable: true, minWidth: 120, showOverflow: true },
          { field: 'txmc', title: '体系名称', sortable: true, minWidth: 120, showOverflow: true },
          { field: 'txgg', title: '体系规格', minWidth: 100, showOverflow: true },
          { field: 'rzbz', title: '认证标准', minWidth: 120, showOverflow: true },
          { field: 'skzt', title: '收款状态', minWidth: 120, showOverflow: true, slots: { default: 'receiveStatus' } },
          { field: 'skqrr', title: '收款确认人', minWidth: 120, showOverflow: true },
          { field: 'skqrsj', title: '收款确认时间', minWidth: 120, showOverflow: true, },
          { field: 'erpbh', title: 'ERP编号', minWidth: 100, showOverflow: true },
          { field: 'sxq', title: '市县区', minWidth: 120, showOverflow: true },
          { field: 'shlx', title: '审核类型', minWidth: 100, showOverflow: true },
          { field: 'rzfw', title: '认证范围', minWidth: 100, showOverflow: true },
          { field: 'txrs', title: '体系人数', minWidth: 80, showOverflow: true },
          { field: 'csje', title: '初审金额', minWidth: 80, showOverflow: true },
          { field: 'jsje', title: '监审金额', minWidth: 80, showOverflow: true },
          {
            field: 'htqdrq', title: '签单日期', sortable: true, minWidth: 120, showOverflow: true,
            formatter: ({ cellValue }) => XEUtils.toDateString(cellValue, 'yyyy-MM-dd')
          },
          { field: 'createtime', title: '创建时间', width: 120, showOverflow: true },
          { field: 'createname', title: '创建人', width: 60, showOverflow: true },
          { field: 'updatetime', title: '更新时间', width: 150, showOverflow: true },
          { field: 'updatename', title: '更新人', width: 60, showOverflow: true },
          { field: 'operation', title: '操作', fixed: 'right', width: 160, slots: { default: 'operation' } },
        ],
        data: []
      },
      PagerConfig: {
        align: 'center',
        pageSize: 25,
        total: 0,
        currentPage: 1,
        pageSizes: [10, 25, 50, 100, 200],
      },
      ChildGridOptions: {
        id:'ProjectReceiveChildGrid',
        border: true,
        stripe: true,
        loading: false,
        size: PageConfig.ComponentSize,
        height: 300,
        columnConfig: {
          resizable: true
        },
        rowConfig: {
          isHover: true,
          isCurrent:true
        },
        editConfig: {
          trigger: 'click',
          mode: 'row',
          showStatus: true
        },
        exportConfig: {
        },
        toolbarConfig: {
          tools: [
            { code: 'childAdd', icon: 'vxe-icon-add', circle: true }
          ]
        },
        customConfig:{
          mode:'drawer',
          storage:true
        },
        columns: [
          { type: 'seq', width: 50 },
          { field: 'skxm', title: '收款项', minWidth: 200, showOverflow: true, editRender: { name: 'VxeInput' } },
          { field: 'ysrq', title: '应收日期', minWidth: 120, showOverflow: true, editRender: { name: 'VxeDatePicker', props: { type: 'date' } } },
          { field: 'skje', title: '应收金额', minWidth: 90, showOverflow: true, editRender: { name: 'VxeInput' } },
          { field: 'skzt', title: '收款状态', minWidth: 90, showOverflow: true, slots: { default: 'receiveStatus' }, editRender: skztItemRender },
          { field: 'ssje', title: '实收金额', minWidth: 90, showOverflow: true, editRender: { name: 'VxeInput' } },
          { field: 'skr', title: '收款人', minWidth: 80, showOverflow: true, editRender: { name: 'VxeInput' } },
          { field: 'skrq', title: '收款日期', minWidth: 150, showOverflow: true, editRender: { name: 'VxeDatePicker', props: { type: 'date' } } },
          { field: 'operation', title: '操作', fixed: 'right', width: 120, slots: { default: 'operation' } },
        ],
        data: []
      },
    }
  },
  created() {
    // 动态头部按钮
    this.btnPromission = getBtnCtr(
      this.$route.path,
      this.$store.getters.userPermission
    )
  },
  mounted(){
    
    
  },
  methods: {
    ValidatorNumber(e) {
      if (!isNumber(e.itemValue)) {
        return new Error('请输入数字');
      } else {
        if (e.itemValue < 0) {
          return new Error('请输入大于等于0的数字');
        }
      }
    },
    handleQuery(e) {
      this.QueryConditions = e;
      this.DoQuery(e);
    },
    handlePageChange() {
      this.DoQuery(this.QueryConditions);
    },
    handleEditModalReset() {

    },
    handleChildCancel() {
      this.$refs.childgrid.clearEdit();
    },
    hasEditStatus(row) {
      return this.$refs.childgrid.isEditByRow(row);
    },
    handleChildSave(row) {
      this.$refs.childgrid.clearEdit();
      if (!row.skxm || row.skxm.length < 1) {
        VxeUI.modal.message({ content: '请填写收款项', status: 'error', duration: this.duration });
        return;
      }
      if (!row.ysrq || row.ysrq.length < 1) {
        VxeUI.modal.message({ content: '请填写应收日期', status: 'error', duration: this.duration });
        return;
      }
      if (!row.skje || row.skje.length < 1) {
        VxeUI.modal.message({ content: '请填写收款金额', status: 'error', duration: this.duration });
        return;
      }
      row.id == 0 ? this.DoChildAdd(row) : this.DoChildEdit(row);
    },
    handleChildToolClick({ code }) {
      if(!this.editRow) {
        VxeUI.modal.message({ content: '请选择要收款的项目', status: 'error', duration: this.duration })
        return;
      }
      if(this.editRow.skzt == 'Y') {
        VxeUI.modal.message({ content: '该项目已确认收款，不能再操作', status: 'error', duration: this.duration })
        return;
      }
      if (code == 'childAdd') {
        this.ChildGridOptions.data.push({
          id: 0,
          xmid: this.editRow.id,
          skxm: '',
          ysrq: '',
          skje: 0,
          skzt: 'N',
          ssje: 0,
          skr: '',
          skrq: ''
        })
      }
    },
    handleReceive(row) {
      this.editRow = row;
      let queryConditions = [{
        column: 'xmid',
        content: row.id,
        condition: '0'
      }]
      this.DoChildQuery(queryConditions);
    },
    handleReceiveConfirm(row) {
      this.AuditReceiveStatus(row);
    },
    handleChildEdit(row) {
      if (row.skzt == 'Y') {
        VxeUI.modal.message({ content: '该款项已收款，不允许编辑', status: 'error', duration: this.duration })
        return;
      }
      this.$refs.childgrid.setEditRow(row);
    },
    handleChildDelete(row) {
      this.DoChildDelete(row);
    },
    DoQuery(e) {
      if (e) {
        e.push({ column: 'shzt', content: 'Y', condition: '0' })
      } else {
        e = [{ column: 'shzt', content: 'Y', condition: '0' }]
      }
      let request = {
        isPaging: true,
        pageSize: this.PagerConfig.pageSize,
        pageIndex: this.PagerConfig.currentPage,
        queryConditions: e,
        orderByConditions: []
      }
      CURDHelper.Query(request, this.BaseApiUrl)
        .then(res => {
          this.GridOptions.data = res.data;
          this.PagerConfig.total = res.totalNumber;
          this.editRow = undefined;
          this.ChildGridOptions.data = []
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    DoChildEdit(row) {
      this.$refs.childgrid.clearEdit();
      let postData = {
        id: row.id,
        ssje: row.ssje,
        skr: row.skr,
        skrq: row.skrq,
        skzt: row.skzt,
      }

      if (postData.skzt == 'Y') {
        if(!postData.ssje || postData.ssje.length < 1) {
          VxeUI.modal.message({ content: '请填写实收金额', status: 'error', duration: this.duration })
          return;
        }
        if(!postData.skr || postData.skr.length < 1) {
          VxeUI.modal.message({ content: '请填写收款人', status: 'error', duration: this.duration })
          return;
        }
        if(!postData.skrq || postData.skrq.length < 1) {
          VxeUI.modal.message({ content: '请填写收款日期', status: 'error', duration: this.duration })
          return; 
        }
        CURDHelper.EditWithConfirm('是否确认该款项已收款，确认后不允许再编辑数据', postData, null, `${this.BaseChildApiUrl}`)
          .then(res => {
            VxeUI.modal.message({ content: '编辑数据成功', status: 'success', duration: this.duration })
            let queryConditions = [{
              column: 'xmid',
              content: this.editRow.id,
              condition: '0'
            }]
            this.DoChildQuery(queryConditions);
          }).catch(err => {
            VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
          })
      } else {
        CURDHelper.Edit(postData, null, `${this.BaseChildApiUrl}`)
          .then(res => {
            VxeUI.modal.message({ content: '编辑数据成功', status: 'success', duration: this.duration })
            let queryConditions = [{
              column: 'xmid',
              content: this.editRow.id,
              condition: '0'
            }]
            this.DoChildQuery(queryConditions);
          }).catch(err => {
            VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
          })
      }
    },
    DoChildQuery(e) {
      let queryConditions = e || [];
      let request = {
        isPaging: false,
        pageSize: 1,
        pageIndex: 1,
        queryConditions: queryConditions,
        orderByConditions: [{
          column: 'createtime',
          condition: 'asc'
        }]
      }
      CURDHelper.Query(request, this.BaseChildApiUrl)
        .then(res => {
          this.ChildGridOptions.data = res.data;
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })
    },
    DoChildAdd(row) {
      this.$refs.childgrid.clearEdit();
      if (row.skzt == 'Y') {
        if(!postData.ssje || postData.ssje.length < 1) {
          VxeUI.modal.message({ content: '请填写实收金额', status: 'error', duration: this.duration })
          return;
        }
        if(!postData.skr || postData.skr.length < 1) {
          VxeUI.modal.message({ content: '请填写收款人', status: 'error', duration: this.duration })
          return;
        }
        if(!postData.skrq || postData.skrq.length < 1) {
          VxeUI.modal.message({ content: '请填写收款日期', status: 'error', duration: this.duration })
          return; 
        }
        CURDHelper.Add('是否确认该款项已收款，确认后不允许再编辑数据', row, null, `${this.BaseChildApiUrl}`)
          .then(res => {
            VxeUI.modal.message({ content: '编辑数据成功', status: 'success', duration: this.duration })
            let queryConditions = [{
              column: 'xmid',
              content: this.editRow.id,
              condition: '0'
            }]
            this.DoChildQuery(queryConditions);
          }).catch(err => {
            VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
          })
      } else {
        CURDHelper.Add(row, null, `${this.BaseChildApiUrl}`)
          .then(res => {
            VxeUI.modal.message({ content: '编辑数据成功', status: 'success', duration: this.duration })
            let queryConditions = [{
              column: 'xmid',
              content: this.editRow.id,
              condition: '0'
            }]
            this.DoChildQuery(queryConditions);
          }).catch(err => {
            VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
          })
      }
    },
    AuditReceiveStatus(row) {
      if (row.skzt == 'Y') {
        VxeUI.modal.message({ content: '该项目已收款确认，无需再操作', status: 'error', duration: this.duration });
        return;
      }
      let postData = {id: row.id,shzt: 'Y'};
      CURDHelper.EditWithConfirm('是否确认该项目已收款完成，确认后不允许再编辑数据', postData, null, `${this.BaseApiUrl}/ReceiveConfirm`)
        .then(res => {
          VxeUI.modal.message({ content: '操作成功', status: 'success', duration: this.duration });          
          this.DoQuery(this.QueryConditions);
        }).catch(err => {
          VxeUI.modal.message({ content: `${err}`, status: 'error', duration: this.duration })
        })

    },
    DoChildDelete(row) {
      //删除
      let deleteModel = {
        ID: row.id,
        DeleteRemark: ''
      };
      CURDHelper.DeleteWithConfirm('是否删除当前数据', deleteModel, null, this.BaseChildApiUrl)
        .then(res => {
          VxeUI.modal.message({content: '删除数据成功',status: 'success',duration: this.duration})
          let queryConditions = [{
            column: 'xmid',
            content: this.editRow.id,
            condition: '0'
          }]
          this.DoChildQuery(queryConditions);
        }).catch(err => {
          VxeUI.modal.message({content: err,status: 'error',duration: this.duration})
        })
    }
  }
}
